﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class QuestManager : MonoBehaviour {
	GameObject QuestCompleteToast;
	GameObject QuestStartDialog;

	private static QuestManager _instance;
	public static QuestManager instance
	{
		get
		{
			if( _instance == null )
				Create();

			return _instance;
		}
	}

    public List<GameObject> touchIndicatorList = new List<GameObject>();

	static public void Create()
	{
		GameObject obj = ResourceLoader.Load("Prefab/Quest/QuestManager") as GameObject;
		GameObject manager = Instantiate(obj) as GameObject;
		manager.name = "QuestManager";
		DontDestroyOnLoad(manager);
		_instance = manager.GetComponent<QuestManager>();
	}
	
	void Awake()
	{
	}

	// Use this for initialization
	void Start () {
		CheckAndStartQuest();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void CheckAndStartQuest()
	{
		if( GameData.Inst.CanStartQuest && UserData.Instance.userModel.questInfo.state == 2 )
			ShowQuestStartDialog();
	}

	void ShowQuestStartDialog()
	{
		if( UserData.Instance.userModel.questInfo.state != 2 )	return;

		DQuestInfo questInfo = GameData.Inst.GetQuestInfo(UserData.Instance.userModel.questInfo.questId);

		if( QuestStartDialog == null )
		{
			GameObject obj = Resources.Load("Prefab/Toast/Toast_Quest_talk") as GameObject;
			QuestStartDialog = Instantiate(obj) as GameObject;
			QuestStartDialog.GetComponent<UIEventTrigger>().onRelease.Add(new EventDelegate(this, "OnClickedCloseDialog"));
			QuestStartDialog.transform.parent = ToastManager.Instance.toastCamera.transform;
			QuestStartDialog.transform.localPosition = new Vector3(0f,-122f,200f);
			QuestStartDialog.transform.localScale = Vector3.one;
		}
		if( QuestStartDialog.transform.FindChild("NPCName") != null )
		{
			UILabel name = QuestStartDialog.transform.FindChild("NPCName").GetComponent<UILabel>();
			if( name != null )
			{
//				name.text = TextController.GetStringWithId(
			}
		}

		Transform dialogTrans = QuestStartDialog.transform.FindChild("talk").FindChild("Scroll").FindChild("Body");
		if( dialogTrans != null )
		{
			UILabel body = dialogTrans.GetComponent<UILabel>();
			if( body != null )
			{
				body.text = TextController.GetStringWithId(string.Format("questScript{0}", UserData.Instance.userModel.questInfo.questId));
				body.ResizeCollider();
				if( body.collider.bounds.size.y > 260 )
					QuestStartDialog.transform.FindChild("talk").FindChild("arrow").gameObject.SetActive(true);
                body.GetComponent<UIEventTrigger>().onPress.Add(new EventDelegate(this, "OnClickedCloseDialog"));
			}
		}
	
		QuestStartDialog.SetActive(true);
		ToastManager.Instance.gameObject.SetActive(true);
	}

	public void OnClickedCloseDialog()
	{
		NetworkManager.Instance.C2S_START_QUEST(ResponseStartQuest, UserData.Instance.userModel.questInfo.questId);
	}

    private void ResponseStartQuest(ResponseModel<QuestResponseModel> result)
	{
        UserData.Instance.userModel.questInfo = result.data;
		if( GameObject.Find("SceneManager") != null )
		{
			GameObject.Find("SceneManager").GetComponent<MainScene>().questGoingMarkObject.SetActive(true);
		}
		QuestStartDialog.SetActive(false);

        ShowQuestCompleteToast();

        SceneController.LoadLevel("Quest");
	}

	public void ResponseGetQuestInfo(ResponseModel<QuestResponseModel> result)
	{
		UserData.Instance.userModel.questInfo = result.data;
		Debug.LogError(result.data.state);

        ShowQuestCompleteToast();
	}

	public bool isAlreadyShownCompleteToast = false;
	public void ShowQuestCompleteToast()
	{
        if (UserData.Instance.userModel.questInfo.state != 4 || isAlreadyShownCompleteToast)
        {
            ToastManager.Instance.ShowQAToastInHoldList();
            return;
        }

        DQuestInfo questInfo = GameData.Inst.GetQuestInfo(UserData.Instance.userModel.questInfo.questId);
        //if (questInfo.objective == 31 || questInfo.objective == 41 || questInfo.objective == 51)
        //    ToastManager.Instance.ShowQuestCompleteToast(true);
        //else
            ToastManager.Instance.ShowQuestCompleteToast(false);
        
		isAlreadyShownCompleteToast = true;

        foreach(GameObject obj in touchIndicatorList)
        {
            if( obj != null )
                obj.SetActive(false);
        }

        touchIndicatorList.Clear();
	}

    public void HideTouchIndicator()
    {
        foreach (GameObject obj in touchIndicatorList)
        {
            if (obj != null)
                obj.SetActive(false);
        }
    }

	public void GotoQuestMainScene()
	{
		SceneController.LoadLevel("Quest");
	}
}
